//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {

    // 数据加载
    loading: true,

  },

  // 页面第一次加载
  onLoad: function (options) {
    this.setData({
      options: options
    })
  },

  // 每次加载都调用
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData: function () {

    var that = this;
    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'buyer_order',
          act: 'view',
          order_id: that.data.options.order_id,
          t_session: session
        },

        // 请求成功
        success: function (res) {
          console.log(res.data);
          var data = res.data;

          if (data.done && data.retval) { // 成功

            var status = '';
            if (data.retval.status == 10) {
              status = '待付款'
            } else if (data.retval.status == 20) {
              status = '待发货'
            } else if (data.retval.status == 30) {
              status = '待收货'
            } else if (data.retval.status == 40) {
              status = '交易成功'
            } else if (data.retval.status == 41) {
              status = '退款中'
            } else if (data.retval.status == 0) {
              status = '交易已取消'
            } else if (data.retval.status == 42) {
              status = '待收货'
            } else if (data.retval.status == 43) {
              status = '交易已取消'
            }

            that.setData({

              // 关闭loading效果
              loading: false,

              // 地址列表
              data: data.retval,

              // 订单状态
              status: status

            });

          } else { // 失败
            that.setData({
              errorTips: data.msg || '请求错误，act：index'
            })
          }

        },

        // 地址列表
        fail: function () {
          that.setData({
            errorTips: '接口请求失败，act：index'
          })
        }
      })
    })

  },

  // 拨打客服电话
  phoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: '02087398787'
    })
  },
  // 确认收货
  confirmOrder: function (event) {
    var that = this;
    var orderId = event.currentTarget.dataset.orderid;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'buyer_order',
          act: 'confirm_order',
          order_id: orderId,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.done) {
            wx.showToast({
              title: data.retval.msg,
              icon: 'success',
              duration: 2000,
              success: function () {
                that.getData();
              }
            })
          }
        }
      })
    })
  },
  //退款
  refund: function (event){
    var that = this;
    var orderId = that.data.options.order_id;
    console.log(orderId);
    app.getLoginSession(function (session) {
      wx.request({
        url: 'https://zazhi.gzgaodu.com//api/index.php?app=buyer_order&act=apply_refund',
        data:{
          order_id:orderId,
          t_session: session
        },
        success:function(res){
          var data = res.data;
          if(data.done){
            console.log(data.retval.msg);
            wx.showModal({
              title: '提示',
              content: data.retval.msg
            })
          }
        }
      })
    })
  }

};

// 注册页面
Page(init);
